<?php
namespace app\admin\controller;
use app\common\controller\Common;
// namespace app\index\module;
use app\common\controller\Adminbase;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;


import('Curl', EXTEND_PATH);
import('Interfaces', EXTEND_PATH);

//class Order extends Adminbase
class Data extends Common{
    public  function my_sort($a,$b)
    {
        if ($a[0]==$b[0]) return 0;
        return ($a[0]<$b[0])?-1:1;
    }
    /**
     * 俱乐部管理
     * @return
     */
    public function dataclub(){

        return $this->fetch();
    }
    /**
     * 俱乐部管理
     * @return
     */
    public function president(){

        return $this->fetch();
    }
    public function president_2(){

        return $this->fetch();
    }

    /**
     * 俱乐部管理
     * @return
     */
    public function dataclub_2(){

        return $this->fetch();
    }
    /**
     * 俱乐部管理
     * @return
     */
    public function dataclub_3(){

        return $this->fetch();
    }
    /**
     * 俱乐部管理
     * @return
     */
    public function regionaldata(){

        return $this->fetch();
    }
    /**
     * 俱乐部管理
     * @return
     */
    public function regionaldata_2(){

        return $this->fetch();
    }
    /**
     * 俱乐部管理
     * @return
     */
    public function dataclub_4(){

        return $this->fetch();
    }
    /**
     * 俱乐部管理
     * @return
     */
    public function dataclub_5(){

        return $this->fetch();
    }
    /**
     * 充值分析
     * @return
     */
    public function dataczfx(){

        return $this->fetch();
    }
    /**
     * 概況
     * @return
     */
    public function datagk(){

        return $this->fetch();
    }
    /**
     * 玩家蓝钻收支表
     * @return
     */
    public function datalz(){

        return $this->fetch();
    }
    /**
     * 玩家黄钻收支表
     * @return
     */
    public function datahj(){

        return $this->fetch();
    }
    /**
     * 招财树（捐赠统计）
     * @return
     */
    public function donation_statistics(){

        return $this->fetch();
    }
    /**
     * 招财树（捐赠明细）
     * @return
     */
    public function donation_details(){

        return $this->fetch();
    }
    /**
     * 留存
     * @return
     */
    public function datalc(){
        if($this->request->isPost()){
            $start_date =input("param.start_date");
            $end_date = input("param.end_date");
            $json = array('login_retention','day_added_user');
            $refer = json_encode($json);
            $sort = [
                'columns'=>['datetime'],
                'order'=>["asc"]
            ];
            $params = array(
                'refer'=>$refer,
                'product'=>PRODUCT,
//                'product'=>PRODUCT,
                'start_date'=>"$start_date",
                'end_date'=>"$end_date",
                'sort'=>json_encode($sort)

            );
            $url =URL.'?'. http_build_query($params);
//            $url ='http://bee.chessvans.com:8000/report'.'?'. http_build_query($params);
            $curl = new \Curl();
            $result = $curl->vget($url);
            $output = json_decode($result);
            $club_data_list = array();
            foreach ($output->result as $key => $value) {
                if (!array_key_exists($value->datetime, $club_data_list)){
                    $club_data_list[$value->datetime] = array();
                }
                if($value->refer == 'day_added_user'){
                    $club_data_list[$value->datetime]['add_user'] = $value->result;
                }else{
                    $club_data_list[$value->datetime][$value->retention_days] = $value->retention_num;
                }
            }
            return json($club_data_list);
        }else{
            return $this->fetch();
        }

    }
    /**
     * 實時數據
     * @return
     */
    public function datass(){

        return $this->fetch();
    }
    /**
     *玩法
     * @return
     */
    public function datawf(){

        return $this->fetch();
    }
    /**
     *玩法
     * @return
     */
    public function dataxw(){

        return $this->fetch();
    }
    /**
     *玩法
     * @return
     */
    public function dataxw_2(){

        return $this->fetch();
    }
    /**
     *玩法
     * @return
     */
    public function datawf_2(){

        return $this->fetch();
    }

    /**
     *金币
     * @return
     */
    public function gold(){

        return $this->fetch();
    }
    /**
     *钻石
     * @return
     */
    public function diamond(){

        return $this->fetch();
    }
    /**
     *俱乐部列表
     * @return
     */
    public function club_list(){
        $where = [];
        $time = input("param.time");
        $cid = input("param.cid");
        $club_type = input("param.club_type");
        $club_name = input("param.club_name");
        $president_id = input("param.president_id");
        if($time !=""){
            $start_time = substr($time,0,10)." "."00:00:00";;
            $end_time = substr($time,13)." "."23:59:59";;
            $where["create_time"] = array(array('Egt',$start_time),array('Elt',$end_time), 'and');
        }
        if($club_name !=""){
            $where['club_name'] = base64_encode($club_name);
        }
        if($cid !=0){
            $where['cid'] =$cid;
        }
        if($president_id !=0){
            $where['president_id'] =$president_id;
        }
        if($club_type !=""){
            $where['club_type'] =$club_type;
        }
        $result = Db::name('club')
            ->where($where)
            ->select();
//        print_r($result);die;
        foreach ($result as $k=>$v){
            $club = model('clubModel');
            $player_id = $v['president_id'];
            $club_id = $v['cid'];
            $type = 2;
//         $gold = $club->gold($player_id,$club_id,$type);
            $diamond = $club->yellow_diamond($player_id,$club_id);
//            $nickname = $club->president_name($player_id);
            $club_members_count = Db::name('user_club')
                ->where("club_id",$v['cid'])
                ->count();
            $result[$k]['number_people'] = $club_members_count.'/'.$v['max_club_members_count'];
            $result[$k]['sum'] = $club->club_dimond($club_id);
            $result[$k]['diamond'] = $diamond;
            $result[$k]['club_name'] = base64_decode($v['club_name']);

        }
//        print_r($result);die;
        return  json($result);
    }
    //vip详情
    public function vip_details(){
        $club_id = input("param.club_id");
        $player_id = input("param.player_id");
        $vip_where = [];
        $vip_where['club_id'] =$club_id;
        $vip_where['player_id'] =$player_id;
//        print_r($vip_where);
        $vip_card = Db::name("club_vip")->where($vip_where)->select();
        foreach ($vip_card as $key =>$value){
            $vip = $value['vid'];
            $vip_name = Db::name("vip_card")->where("vip_id",$vip)->find();
            $vip_card[$key]['vip_name'] = $vip_name['name'];

        }
        return  json($vip_card);
    }
    /**
     *捐赠明细
     * @return
     */
    public function user_sendgold(){
        $time = input("param.time");
        $cid = input("param.cid");
        $player_id = input("param.player_id");
        $where = [];
        if($time !=""){
            $start_time = substr($time,0,10)." "."00:00:00";
            $end_time = substr($time,13)." "."23:59:59";
            $where["send_time"] = array(array('Egt',$start_time),array('Elt',$end_time), 'and');
        }
        if($cid !=0){
            $where['cid'] =$cid;
        }
        if($player_id !=0){
            $where['player_id'] =$player_id;
        }
        $result =  Db::name('user_sendgold')
            ->where($where)
            ->select();
        foreach ($result as $k=>$v){
            $club_id =$v['club_id'];
            $message_url =USER_HTTP."api/get_batch_info.php";
            $message_data = [
                'app_id'=>APP_ID,
                'uid'=>$v['player_id'],
            ];
            $message_data = json_encode($message_data);
            $curl = new \Curl();
            $message_result = $curl->vpost($message_url,$message_data);
            $message_obj =json_decode($message_result);
            $nickname = "";
            foreach ($message_obj->data as $ks=>$vs){
                $nickname =   $vs->nickname;
            }
            $club_name = Db::name('club')->where('cid',$club_id)->find();
            $result[$k]['nickname'] = $nickname;
            $result[$k]['club_name'] = base64_decode($club_name['club_name']);
        }
        return json($result);
    }
    //获取实时数据-游戏指标 ajax接口
    public function get_whilst_game_norm(){
        $refer = array('real_time_data');
        $param['size'] ='1';
        $res = $this -> get_date_statistic_data1($refer, $param);
        $res = array_reverse($res);

        $param1['end_date'] = date("Y-m-d H:i:s",strtotime("-1 day"));
        $param1['size'] ='1';
        $res1 = $this -> get_date_statistic_data1($refer, $param1);
        $res1 = array_reverse($res1);

        $result = new \stdClass();
        $result->day = $res;
        $result->yesterday = $res1;
        return json($result);
    }
    //获取实时数据-游戏指标 图表数据 ajax接口
    public function get_whilst_game_norm_charts(){
        // $url = C('whilst_game_norm_charts');
        $date =input('date', '', 'trim');
        $time = explode(',',$date);
        // print_r(count($time));die;
        for($i=0;$i<count($time);$i++){
            $start_time = $time[$i].' '.'00:00:00';
            $end_time = $time[$i].' '.'23:59:59';
            $param['start_date'] =$start_time;
            $param['end_date'] =$end_time;
            $refer = array('real_time_data');
            $res = $this -> get_date_statistic_data1($refer, $param);
            $data[] = array_reverse($res);
        }
        return json($data);

    }
    /*
        日活跃用户	day_active_user
        日新增用户	day_added_user
        有效用户数	valid_active_user
        开桌数	create_room_num
        完整桌数	finish_room_num
        充值用户总数	recharge_total_user
        充值总金额	recharge_total_amount
        充蓝钻用户数	recharge_gold_user
        充蓝钻金额	recharge_gold_amount
        充黄钻会长数	recharge_diamond_user
        充黄钻金额	recharge_diamond_amount
        用户购卡用户数	recharge_user_card_user
        用户购卡金额	recharge_user_card_amount
        会长购卡会长数	recharge_club_card_user
        会长购卡金额	recharge_club_card_amount
        各玩法单局时长众数 most_table_time_type_detail
    */

    //获取概况
    public function Overview(){
        $start_date =input("param.start_time");
        $end_date = input("param.end_time");
        $json = array('day_active_user','day_added_user','valid_active_user','create_room_num','finish_room_num','recharge_total_user','recharge_total_amount');
        $refer = json_encode($json);
        $sort = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $params = array(
            'refer'=>$refer,
            'product'=>PRODUCT,
            'start_date'=>"$start_date",
            'end_date'=>"$end_date",
            'sort'=>json_encode($sort)

        );
        $url =URL.'?'. http_build_query($params);
        $curl = new \Curl();
        $result = $curl->vget($url);
        $output = json_decode($result);
        $club_data_list = array();
        $item = array();
        $a = array();
        $b = array();
        foreach ($output->result as $k => $v) {
            array_push($a,(array)$v);
        }
        foreach($a as $k=>$v){
            if(!isset($item[$v['datetime']])){
                $item[$v['datetime']]=[$v];
            }else{
                array_push($item[$v['datetime']],$v);
            }
        }

        foreach ($item as $key => $value) {
            $b['day_added_user'] = 0;
            $b['day_active_user'] = 0;
            $b['valid_active_user'] = 0;
            $b['recharge_total_amount'] = 0;
            $b['recharge_total_user'] = 0;
            $b['create_room_num'] = 0;
            $b['finish_room_num'] = 0;
            foreach ($value as $e => $a) {
                $b['datetime'] = $a['datetime'];
                if($a['refer'] == 'day_added_user'){
                    if(!empty($a['result'])){
                        $b['day_added_user'] = $a['result'];//日新增用户
                    }else{
                        $b['day_added_user'] = 0;
                    }
                }
                if($a['refer'] == 'day_active_user'){
                    if(!empty($a['result'])){
                        $b['day_active_user'] = $a['result'];//日活跃用户
                    }else{
                        $b['day_active_user'] = 0;
                    }
                }
                if($a['refer'] == 'valid_active_user'){
                    if(!empty($a['result'])){
                        $b['valid_active_user'] = $a['result'];//有效用户数
                    }else{
                        $b['valid_active_user'] = 0;
                    }
                }
                if($a['refer'] == 'recharge_total_amount'){
                    if(!empty($a['result'])){
                        $b['recharge_total_amount'] = round($a['result']/100,2);//充值金额
                    }else{
                        $b['recharge_total_amount'] = 0;
                    }
                }
                if($a['refer'] == 'recharge_total_user'){
                    if(!empty($a['result'])){
                        $b['recharge_total_user'] = $a['result'];//充值人数
                    }else{
                        $b['recharge_total_user'] = 0;
                    }
                }
//
                if($a['refer'] == 'create_room_num'){
                    if(!empty($a['result'])){
                        $b['create_room_num'] = $a['result'];//总开桌数
                    }else{
                        $b['create_room_num'] = 0;
                    }
                }
                if($a['refer'] == 'finish_room_num'){
                    if(!empty($a['result'])){
                        $b['finish_room_num'] = $a['result'];//总完成桌数
                    }else{
                        $b['finish_room_num'] = 0;
                    }
                }

            }
            array_push($club_data_list,$b);
        }
        return json($club_data_list);

    }
    //实时在线
    public function real_time_data(){

        $json = array('real_time_data');
        $refer = json_encode($json);
        $sort = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $params = array(
            'refer'=>$refer,
            'product'=>PRODUCT,
            'size'=>'1'
//                 'sort'=>json_encode($sort)
        );
//            $url ='http://58a550645aa4d78e64a41ce7aa6beec1.xianlaigame.com:8000/report'.'?'. http_build_query($params);
        $url =URL.'?'. http_build_query($params);
        $curl = new \Curl();
        $result = $curl->vget($url);
        $output = json_decode($result,true);
        $uid =  Session::get("uid");
        $area = Db::name("users")->where("id",$uid)->find();
        $area_id = explode(',',$area['area_id']);

        foreach ($output['result'][0]['region_details'] as $k=>$v){
                $area = Db::name("area")->where("aid", $k)->find();
                $area_name = $area['area_name'];
                $output['result'][0]['region_details'][$k]['club_recharge_card_amount']= $v['club_recharge_card_amount']/100;
                if(isset($v['club_recharge_diamond_amount'])){
                    $output['result'][0]['region_details'][$k]['club_recharge_diamond_amount']= $v['club_recharge_diamond_amount']/100;// 会长买钻金额
                }else{
                    $output['result'][0]['region_details'][$k]['club_recharge_diamond_amount'] = 0;
                }

                $output['result'][0]['region_details'][$k]['area_name']= $area_name;
//                if(in_array($k,$area_id)){
//                    $a[] = $v;
//                }
        }
        foreach ($output['result'][0]['region_details'] as $k=>$v){
            if(in_array($k,$area_id)){
                    $a[] = $v;
                }
        }
//        print_r($a);
//        die;
        foreach ($output['result'] as $k =>$v){
//            recharge_diamond_amount 购黄钻金额
            $output['result'][$k]['recharge_amount'] =  round($v['recharge_amount']/100,2);
            $output['result'][$k]['recharge_gold_amount'] =  round($v['recharge_gold_amount']/100,2);
            $output['result'][$k]['recharge_diamond_amount'] =  round($v['recharge_diamond_amount']/100,2);
            $output['result'][$k]['recharge_user_card_amount'] =  round($v['recharge_user_card_amount']/100,2);
            $output['result'][$k]['recharge_club_card_amount'] =  round($v['recharge_club_card_amount']/100,2);
//            print_r();
        }
        foreach ($output['result'][0]['room_type_details'] as $k=>$v){
            $play= Db::name("play")->where("id", $k)->find();
            $play_name = $play['name'];
//            $output['result'][0]['room_type_details'][$k]['club_recharge_card_amount']= $v['club_recharge_card_amount']/100;
            $output['result'][0]['room_type_details'][$k]['play_name']= $play_name;
//                if(in_array($k,$area_id)){
//                    $a[] = $v;
//                }
        }
        foreach ($output['result'][0]['room_type_details'] as $k=>$v){
            if(in_array($k,$area_id)){
                $b[] = $v;
            }
        }
//        print_r($a);
//        die;
        if(empty($a)){
            $a = [];
        }
        if(empty($b)){
            $b = [];
        }
        $result = new \stdClass();
        $result->real_time_data = $output['result'];
        $result->region_detail = $a;
        $result->room_type_details = $b;
//        print_r($result);die;
//        region_details 区域详情
//        {
//            region_id 区域id
//        day_active_user 区域日活
//        start_room_num 区域开桌数
//        finish_room_num 区域完成桌数
//        added_club_num 新增亲友圈数
//        reduce_pay_gold_num 充值蓝钻消耗数
//        reduce_free_gold_num 赠送蓝钻消耗数
//        club_recharge_card_amount 会长买卡金额
//        user_recharge_card_amount 用户买卡金额
//        club_recharge_diamond_amount 会长买钻金额
//        user_recharge_diamond_amount 用户买钻金额
        return json($result);
    }
    //获取玩法 开桌
    public function Play_Open_table(){
        $start_date =input("param.start_time");
        $end_date = input("param.end_time");
        $room_type_id = input("param.room_type_id");
//        $room_type_id = 11;
        $json = array('avg_room_num_type_detail','create_room_num_type_detail','finish_room_num_type_detail','valid_user_num_type_detail');
        $refer = json_encode($json);
        $sort = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $params = array(
            'refer'=>$refer,
            'product'=>PRODUCT,
            'start_date'=>"$start_date",
            'end_date'=>"$end_date",
            'room_type_id'=>$room_type_id,
            'sort'=>json_encode($sort)

        );
        $url =URL.'?'. http_build_query($params);
        $curl = new \Curl();
        $result = $curl->vget($url);
        $output = json_decode($result);
        $club_data_list = array();
        $item = array();
        $a = array();
        $b = array();
        $table_type = array();
        foreach ($output->result as $k => $v) {
            array_push($a,(array)$v);
        }
        foreach($a as $k=>$v){
            if(!isset($item[$v['datetime']])){
                $item[$v['datetime']]=[$v];
            }else{
                array_push($item[$v['datetime']],$v);
            }
        };
        /*
        各玩法单局时长众数	most_table_time_type_detail
        各玩法人均桌数	avg_room_num_type_detail
        各玩法开桌数	create_room_num_type_detail
        各玩法完整桌数	finish_room_num_type_detail
        各玩法参与人数	valid_user_num_type_detail
        */

//create_room_num_type_detail
//finish_room_num_type_detail
//valid_user_num_type_detail
//        print_r($a);die;
        foreach ($item as $key => $value) {
            $b['diy'] = 0;
            $b['match'] = 0;
            foreach ($value as $e => $a) {
                $b['datetime'] =  $a['datetime'];
                $b['room_type_id'] = $a['room_type_id'];
                $b['room_type_id'] = $a['room_type_id'];
                $b['room_type_name'] = $a['room_type_name'];
                if($a['refer'] == 'avg_room_num_type_detail'){
                    $b['avg_room_num'] = round($a['avg_room_num'],2);//各玩法人均桌数

                }
                if($a['refer'] == 'create_room_num_type_detail'){
                    $b['create_room_num'] = $a['create_room_num'];//各玩法开桌数

                }
                if($a['refer'] == 'finish_room_num_type_detail'){
                    $b['finish_room_num'] = $a['finish_room_num'];//各玩法完整桌数

                }
                if($a['refer'] == 'valid_user_num_type_detail'){
                    $b['valid_user_num'] = $a['valid_user_num'];//各玩法参与人数

                }
                if($a['refer'] == 'most_table_time_type_detail'){
                    $b['most_table_time'] = $a['most_table_time'];//各玩法单局时长众数

                }

//                if(!isset($b['diy'])){
//                    $b['diy'] = 0;
//                }
//                if(!isset($b['match'])){
//                    $b['match'] = 0;
//                }
            }
            array_push($club_data_list,$b);
        }
//        print_r($club_data_list);
        return json($club_data_list);
    }
    //获取玩法 番型
    public function Play_Shape(){
        $start_date =input("param.start_time");
        $end_date = input("param.end_time");
        $json = array('finish_table_num','finish_table_type_detail');
        $refer = json_encode($json);
        $sort = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $params = array(
            'refer'=>$refer,
            'product'=>'tai_wan_ma_jiang',
            'start_date'=>"$start_date",
            'end_date'=>"$end_date",
            'sort'=>json_encode($sort)

        );
        $url =URL.'?'. http_build_query($params);
        $curl = new \Curl();
        $result = $curl->vget($url);
        $output = json_decode($result);
        $club_data_list = array();
        $item = array();
        $a = array();
        $b = array();
        $c = array();
        foreach ($output->result as $k => $v) {
            array_push($a,(array)$v);
        }
        foreach($a as $k=>$v){
            if(!isset($item[$v['datetime']])){
                $item[$v['datetime']]=[$v];
            }else{
                array_push($item[$v['datetime']],$v);
            }
        }
        foreach ($item as $key => $value) {
            array_splice($c, 0, count($c));
            foreach ($value as $e => $a) {
                $b['datetime'] = $a['datetime'];
                if($a['refer'] =='finish_table_num'){
                    $b['finish_table_num'] = $a['result'];
                }
                if($a['refer'] =='finish_table_type_detail'){
                    $type = $a['win_type'].':'.$a['finish_table_num'].'局';
                    array_push($c,$type);
                }
                $b['finish_table_type_detail'] =$c;
            }
            array_push($club_data_list,$b);
        }
        return json($club_data_list);
    }
    //充值分析
    public function Recharge_analysis(){
        $start_date =input("param.start_time");
        $end_date = input("param.end_time");
        $json = array('recharge_total_amount','recharge_total_user','recharge_diamond_amount','recharge_diamond_user','recharge_gold_amount','recharge_gold_user');
        $refer = json_encode($json);
        $sort = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $params = array(
            'refer'=>$refer,
            'product'=>'tai_wan_ma_jiang',
            'start_date'=>"$start_date",
            'end_date'=>"$end_date",
            'sort'=>json_encode($sort)

        );
        $url =URL.'?'. http_build_query($params);
        $curl = new \Curl();
        $result = $curl->vget($url);
        $output = json_decode($result);
        $club_data_list = array();
        $item = array();
        $a = array();
        $b = array();
        $c = array();
        foreach ($output->result as $k => $v) {
            array_push($a,(array)$v);
        }
        foreach($a as $k=>$v){
            if(!isset($item[$v['datetime']])){
                $item[$v['datetime']]=[$v];
            }else{
                array_push($item[$v['datetime']],$v);
            }
        }
//        print_r($item);die;
        foreach ($item as $key => $value) {
            foreach ($value as $e => $a) {
                $b['datetime'] = $a['datetime'];
                if($a['refer'] =='recharge_total_amount'){
                    $b['recharge_total_amount'] = $a['result'];//充值总金额
                }
                if($a['refer'] =='recharge_total_user'){
                    $b['recharge_total_user'] = $a['result'];//充值人数
                }
                if($a['refer'] =='recharge_diamond_amount'){
                    $b['recharge_diamond_amount'] = $a['result'];//充钻石总金额
                }
                if($a['refer'] =='recharge_diamond_user'){
                    $b['recharge_diamond_user'] = $a['result'];//充钻石总人数
                }
                if($a['refer'] =='recharge_gold_amount'){
                    $b['recharge_gold_amount'] = $a['result'];//充金币总金额
                }
                if($a['refer'] =='recharge_gold_user'){
                    $b['recharge_gold_user'] = $a['result'];//充金币总人数
                }
            }
            array_push($club_data_list,$b);
        }
        return json($club_data_list);
    }
    //玩家行为分析 新增玩家
    public function Add_player(){
        $start_date =input("param.start_time");
        $end_date = input("param.end_time");
        $json = array('day_added_user','valid_added_user');
        $refer = json_encode($json);
        $sort = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $params = array(
            'refer'=>$refer,
            'product'=>'tai_wan_ma_jiang',
            'start_date'=>"$start_date",
            'end_date'=>"$end_date",
            'sort'=>json_encode($sort)

        );
        $url =URL.'?'. http_build_query($params);
        $curl = new \Curl();
        $result = $curl->vget($url);
        $output = json_decode($result);
        $club_data_list = array();
        $item = array();
        $a = array();
        $b = array();
        $c = array();
        foreach ($output->result as $k => $v) {
            array_push($a,(array)$v);
        }
        foreach($a as $k=>$v){
            if(!isset($item[$v['datetime']])){
                $item[$v['datetime']]=[$v];
            }else{
                array_push($item[$v['datetime']],$v);
            }
        }
        foreach ($item as $key => $value) {
            foreach ($value as $e => $a) {
                $b['datetime'] = $a['datetime'];
                if($a['refer'] =='day_added_user'){
                    $b['day_added_user'] = $a['result'];
                }
                if($a['refer'] =='valid_added_user'){
                    $b['valid_added_user'] = $a['result'];
                }
//
            }
            $b['zero'] = $b['day_added_user'] - $b['valid_added_user'];
            array_push($club_data_list,$b);
        }
//        print_r($club_data_list);
        return json($club_data_list);
    }
    //玩家行为分析 日活玩家
    public function Daily_life(){
        $start_date =input("param.start_time");
        $end_date = input("param.end_time");
        $json = array('day_active_user','valid_active_user');
        $refer = json_encode($json);
        $sort = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $params = array(
            'refer'=>$refer,
            'product'=>'tai_wan_ma_jiang',
            'start_date'=>"$start_date",
            'end_date'=>"$end_date",
            'sort'=>json_encode($sort)

        );
        $url =URL.'?'. http_build_query($params);
        $curl = new \Curl();
        $result = $curl->vget($url);
        $output = json_decode($result);
        $club_data_list = array();
        $item = array();
        $a = array();
        $b = array();
        $c = array();
        foreach ($output->result as $k => $v) {
            array_push($a,(array)$v);
        }
        foreach($a as $k=>$v){
            if(!isset($item[$v['datetime']])){
                $item[$v['datetime']]=[$v];
            }else{
                array_push($item[$v['datetime']],$v);
            }
        }
        foreach ($item as $key => $value) {
            foreach ($value as $e => $a) {
                $b['datetime'] = $a['datetime'];
                if($a['refer'] =='day_active_user'){
                    $b['day_active_user'] = $a['result'];
                }
                if($a['refer'] =='valid_active_user'){
                    $b['valid_active_user'] = $a['result'];
                }
//
            }
            $b['zero'] = $b['day_active_user']- $b['valid_active_user'];
            array_push($club_data_list,$b);
        }
//        print_r($club_data_list);
        return json($club_data_list);
    }
    //牌局统计
    public function Hand(){
        $start_date =input("param.start_time");
        $end_date = input("param.end_time");
        $club_id = input("param.club_id");
        $json = array('room_club_detail');
        $refer = json_encode($json);
        $sort = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $params = array(
            'refer'=>$refer,
            'product'=>PRODUCT,
            'start_date'=>"$start_date",
            'end_date'=>"$end_date",
            'club_id'=>"$club_id",
            'sort'=>json_encode($sort)

        );
        $url =URL.'?'. http_build_query($params);
        $curl = new \Curl();
        $result = $curl->vget($url);
        $output = json_decode($result);
        $club_data_list = array();
        $item = array();
        $a = array();
        $b = array();
        $c = array();
//        print_r($params);die;
        foreach ($output->result as $k => $v) {
            array_push($a,(array)$v);
        }
        foreach($a as $k=>$v){
            if(!isset($item[$v['datetime']])){
                $item[$v['datetime']]=[$v];
            }else{
                array_push($item[$v['datetime']],$v);
            }
        }
        /*club_id 俱乐部id
        club_name 俱乐部名
        club_region_id 俱乐部地域id
        create_room_num 开桌数
        valid_user_num 参与人数
        reduce_gold_num 消耗蓝钻数*/

        foreach ($item as $key => $value) {
            $create_room_num = 0;
            $valid_user_num = 0;
            $reduce_gold_num = 0;
            foreach ($value as $e => $a) {
                if($key  == $a['datetime']){
                    $b['datetime'] = $a['datetime'];
                    $create_room_num = $a['create_room_num'];
                    $valid_user_num = $a['valid_user_num'];
                    $reduce_gold_num = $a['reduce_gold_num'];
                    if( $reduce_gold_num == "NULL"){
                        $reduce_gold_num = 0;
                    }
                    $b['create_room_num'] =  $create_room_num; //开桌数
                    $b['valid_user_num'] =  $valid_user_num ;//参与人数 活跃玩家
                    $b['reduce_gold_num'] =  $reduce_gold_num ;//消耗蓝钻数
                    if(isset($a['reduce_pay_gold_num'])){
                        $b['reduce_pay_gold_num'] = $a['reduce_pay_gold_num'];;//消耗充值蓝钻数
                    }else{
                        $b['reduce_pay_gold_num'] = 0;
                    }
                    if(isset($a['reduce_free_gold_num'])){
                        $b['reduce_free_gold_num'] =  $a['reduce_free_gold_num']; ;//消耗赠送蓝钻数
                    }else{
                        $b['reduce_pay_gold_num'] = 0;
                    }

                    $b['club_id'] =  $a['club_id'];
                    $b['club_name'] =  $a['club_name'];

                }

            }
            array_push($club_data_list,$b);
        }
        return json($club_data_list);
    }
//    //牌局日期统计
//    public function Hand_date(){
//        $start_date =input("param.start_time");
//        $end_date = input("param.end_time");
//        $club_id = input("param.club_id");
//        $json = array('finish_table_club_detail');
//        $refer = json_encode($json);
//        $sort = [
//            'columns'=>['datetime'],
//            'order'=>["desc"]
//        ];
//        $params = array(
//            'refer'=>$refer,
//            'product'=>'tai_wan_ma_jiang',
//            'start_date'=>"$start_date",
//            'end_date'=>"$end_date",
//            'club_id'=>"$club_id",
//            'sort'=>json_encode($sort)
//
//        );
//        $url =URL.'?'. http_build_query($params);
//        $curl = new \Curl();
//        $result = $curl->vget($url);
//        $output = json_decode($result);
//        $club_data_list = array();
//        $item = array();
//        $a = array();
//        $b = array();
//        $c = array();
//        foreach ($output->result as $k => $v) {
//            array_push($a,(array)$v);
//        }
//        foreach($a as $k=>$v){
//            if(!isset($item[$v['datetime']])){
//                $item[$v['datetime']]=[$v];
//            }else{
//                array_push($item[$v['datetime']],$v);
//            }
//        }
//        /*club_id 俱乐部id
//        club_name 俱乐部名
//        club_region_id 俱乐部地域id
//        create_room_num 开桌数
//        valid_user_num 参与人数
//        reduce_gold_num 消耗蓝钻数*/
//
////        foreach ($item as $key => $value) {
////            $finish_table_num = 0;
////            $valid_user_num = 0;
////            foreach ($value as $e => $a) {
////                if($key  == $a['datetime']){
////                    $b['datetime'] = $a['datetime'];
////                    $finish_table_num = $a['finish_table_num'];
////                    $valid_user_num = $a['valid_user_num'];
////                    $b['finish_table_num'] =  $finish_table_num;
////                    $b['valid_user_num'] =  $valid_user_num ;
////                    $b['club_id'] = $a['club_id'];
////                    $b['club_name'] = $a['club_name'];
////
////                }
////
////            }
////            array_push($club_data_list,$b);
////        }
//        return json($club_data_list);
//    }
    //金币统计
    public function Gold_coin_statistics(){
        $start_date =input("param.start_time");
        $end_date = input("param.end_time");
        $club_id = input("param.club_id");

        $json = array('vip_card_club_detail');
        $refer = json_encode($json);
        $sort = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $params = array(
            'refer'=>$refer,
            'product'=>PRODUCT,
            'start_date'=>"$start_date",
            'end_date'=>"$end_date",
            'club_id'=>$club_id,
            'sort'=>json_encode($sort)

        );
        $url =URL.'?'. http_build_query($params);
        $curl = new \Curl();
        $result = $curl->vget($url);
        $output = json_decode($result);
        $club_data_list = array();
        $item = array();
        $a = array();
        $b = array();
        $c = array();
        $d = array();
        foreach ($output->result as $k => $v) {
            array_push($a,(array)$v);
        }
        foreach($a as $k=>$v){
            if(!isset($item[$v['datetime']])){
                $item[$v['datetime']]=[$v];
            }else{
                array_push($item[$v['datetime']],$v);
            }
        }
        foreach ($item as $k =>$v){
            array_splice($c, 0, count($c));
            array_splice($d, 0, count($d));
            foreach ($v as $e=>$f){
                $b['datetime'] = $f['datetime'];
                $b['club_name'] = $f['club_name'];
//                $b['sell'] = [$f['card_name'].':'.$f['recharge_user_card_num']]; //用户购卡数量(卖出)
//                $b['buy'] = [$f['card_name'].':'.$f['recharge_club_card_num']]; //会长购卡数量(买入）
                $recharge_user_card_num = $f['card_name'].':'.$f['recharge_user_card_num'];
                array_push($c,$recharge_user_card_num);
                $b['sell'] =$c;
                $recharge_club_card_num = $f['card_name'].':'.$f['recharge_club_card_num'];
                array_push($d,$recharge_club_card_num);
                $b['buy'] =$d;
            }
            array_push($club_data_list,$b);
        }

        return json($club_data_list);
    }
    //钻石统计
    public function Diamond_coin_statistics(){
        $club_id = input("param.club_id");

        $start_date =input("param.start_time");
        $end_date = input("param.end_time");
        $json = array('diamond_club_detail');
        $refer = json_encode($json);
        $sort = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $params = array(
            'refer'=>$refer,
            'product'=>PRODUCT,
            'start_date'=>"$start_date",
            'end_date'=>"$end_date",
            'club_id'=>json_encode([$club_id]),
            'sort'=>json_encode($sort)

        );
        $url =URL.'?'. http_build_query($params);
        $curl = new \Curl();
        $result = $curl->vget($url);

        $output = json_decode($result);
        $club_data_list = array();
        $item = array();
        $a = array();
        $b = array();
        $c = array();
        foreach ($output->result as $k => $v) {
            array_push($a,(array)$v);
        }
//        foreach($a as $k=>$v){
//            if(!isset($item[$v['club_id']])){
//                $item[$v['club_id']]=[$v];
//            }else{
//                array_push($item[$v['club_id']],$v);
//            }
//        }
        foreach($a as $k=>$v){
            if(!isset($item[$v['datetime']])){
                $item[$v['datetime']]=[$v];
            }else{
                array_push($item[$v['datetime']],$v);
            }
        }
//        print_r($item);
        /* club_id 俱乐部id
         club_name 俱乐部名
         club_region_id 俱乐部地域id
         user_id 会长id
         user_name 会长名称
         recharge_diamond_num 充黄钻数量(获得)
         recharge_diamond_num 黄钻消耗数量(消耗)*/
//        print_r($item);die;
        foreach ($item as $key => $value) {
            $b['rebate_diamond_num'] = 0;
            $b['reduce_diamond_num'] = 0;
            $b['recharge_diamond_num'] = 0;
            foreach ($value as $e => $a) {
                $b['datetime'] =  $a['datetime'];
                $b['club_id'] = $a['club_id'];
                $b['club_name'] = $a['club_name'];
                $b['user_id'] = $a['user_id'];
                $b['user_name'] = $a['user_name'];
                if(isset($a['reduce_diamond_num'])){
                    $b['reduce_diamond_num'] = $a['reduce_diamond_num'];
                }
                if(isset($a['recharge_diamond_num'])){
                    $b['recharge_diamond_num'] = $a['recharge_diamond_num'];
                }

//                if($a['refer'] == 'diamond_reduce_club_detail'){
//                    $b['reduce_diamond_num'] += $a['reduce_diamond_num'];
//                }
//                if($a['refer'] == 'recharge_diamond_club_detail'){
//                    $b['recharge_diamond_num'] += $a['recharge_diamond_num'];
//                }
//                if($a['refer'] == 'rebate_diamond_club_detail'){
//                    $b['rebate_diamond_num'] += $a['rebate_diamond_num'];
//                }
            }
            if(!isset( $b['reduce_diamond_num'])){
                $b['reduce_diamond_num'] = 0;
            }
            if(!isset( $b['recharge_diamond_num'])){
                $b['recharge_diamond_num'] = 0;
            }
            if(!isset($b['rebate_diamond_num'])){
                $b['rebate_diamond_num'] = 0;
            }
//            $b['get_diamond'] =  $b['recharge_diamond_num']+$b['rebate_diamond_num'];
            array_push($club_data_list,$b);
        }
//         print_r($club_data_list);
        return json($club_data_list);
    }
    //捐赠统计
    public function Donation_statistics_c(){
        $start_date =input("param.start_time");
        $end_date = input("param.end_time");
        $club_id = input("param.club_id");
        $json = array('gold_tree_club_detail');
        $refer = json_encode($json);
        $sort = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $params = array(
            'refer'=>$refer,
            'product'=>'tai_wan_ma_jiang',
            'start_date'=>"$start_date",
            'end_date'=>"$end_date",
            'club_id'=>$club_id,
            'sort'=>json_encode($sort)

        );
        $url =URL.'?'. http_build_query($params);
        $curl = new \Curl();
        $result = $curl->vget($url);
        $output = json_decode($result);
        $club_data_list = array();
        $item = array();
        $a = array();
        $b = array();
        $c = array();
        foreach ($output->result as $k => $v) {
            array_push($a,(array)$v);
        }
        foreach($a as $k=>$v){
            if(!isset($item[$v['datetime']])){
                $item[$v['datetime']]=[$v];
            }else{
                array_push($item[$v['datetime']],$v);
            }
        }
        foreach ($item as $key => $value) {
            $b['get_gold_num'] = 0;
            $b['get_user_num'] = 0;
            $b['opening_gold_num'] = 0;
            $b['opening_user_num'] = 0;
            foreach ($value as $e => $a) {
                $b['datetime'] = $a['datetime'];
                if($a['refer'] == 'gold_tree_club_detail'){
                    if($a['event_type'] == 'get'){
                        if(!empty($a['gold_num'])){
                            $b['get_gold_num'] += $a['gold_num'];
                        }else{
                            $b['get_gold_num'] = 0;
                        }
                        if(!empty($a['user_num'])){
                            $b['get_user_num'] += $a['user_num'];
                        }else{
                            $b['get_user_num'] = 0;
                        }

                    }
                    if($a['event_type'] == 'put'){
                        if(!empty($a['gold_num'])){
                            $b['opening_gold_num'] += $a['gold_num'];
                        }else{
                            $b['opening_gold_num'] = 0;
                        }
                        if(!empty($a['user_num'])){
                            $b['opening_user_num'] += $a['user_num'];
                        }else{
                            $b['opening_user_num'] = 0;
                        }

                    }
                }
            }
            $club_name = Db::name("club")
                ->where("cid",$a['club_id'])
                ->find();
            $b['club_name'] =base64_decode($club_name['club_name']);
            if(!isset( $b['get_gold_num'])){
                $b['get_gold_num'] = 0;
            }
            if(!isset( $b['get_user_num'])){
                $b['get_user_num'] = 0;
            }
            if(!isset( $b['opening_gold_num'])){
                $b['opening_gold_num'] = 0;
            }
            if(!isset( $b['opening_user_num'])){
                $b['opening_user_num'] = 0;
            }
            array_push($club_data_list,$b);
        }
//        print_r($club_data_list);
        return json($club_data_list);
    }
    //捐赠统计
    public function Donation_statistics_b(){
        $start_date =input("param.start_time");
        $end_date = input("param.end_time");
        $json = array('gold_tree_club_detail');
        $refer = json_encode($json);
        $sort = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $params = array(
            'refer'=>$refer,
            'product'=>'tai_wan_ma_jiang',
            'start_date'=>"$start_date",
            'end_date'=>"$end_date",
            'sort'=>json_encode($sort)

        );
        $url =URL.'?'. http_build_query($params);
        $curl = new \Curl();
        $result = $curl->vget($url);
        $output = json_decode($result);
        $club_data_list = array();
        $item = array();
        $a = array();
        $b = array();
        $c = array();
        foreach ($output->result as $k => $v) {
            array_push($a,(array)$v);
        }
        foreach($a as $k=>$v){
            if(!isset($item[$v['club_id']])){
                $item[$v['club_id']]=[$v];
            }else{
                array_push($item[$v['club_id']],$v);
            }
        }
//        club_id 俱乐部id
//        event_type 捐赠or领取
//        gold_num 金币数
//        user_num 用户数
//        print_r($item);die;
        foreach ($item as $key => $value) {
            foreach ($value as $e => $a) {
                $b['club_id'] = $a['club_id'];
                if($a['refer'] == 'gold_tree_club_detail'){
                    if($a['event_type'] == 'get'){
                        if(!empty($a['gold_num'])){
                            $b['get_gold_num'] = $a['gold_num'];
                        }else{
                            $b['get_gold_num'] = 0;
                        }
                        if(!empty($a['user_num'])){
                            $b['get_user_num'] = $a['user_num'];
                        }else{
                            $b['get_user_num'] = 0;
                        }

                    }
                    if($a['event_type'] == 'put'){
                        if(!empty($a['gold_num'])){
                            $b['opening_gold_num'] = $a['gold_num'];
                        }else{
                            $b['opening_gold_num'] = 0;
                        }
                        if(!empty($a['user_num'])){
                            $b['opening_user_num'] = $a['user_num'];
                        }else{
                            $b['opening_user_num'] = 0;
                        }

                    }
                }
            }
            $club_name = Db::name("club")
                ->where("cid",$a['club_id'])
                ->find();
            $b['club_name'] =base64_decode($club_name['club_name']);
            if(!isset( $b['get_gold_num'])){
                $b['get_gold_num'] = 0;
            }
            if(!isset( $b['get_user_num'])){
                $b['get_user_num'] = 0;
            }
            if(!isset( $b['opening_gold_num'])){
                $b['opening_gold_num'] = 0;
            }
            if(!isset( $b['opening_user_num'])){
                $b['opening_user_num'] = 0;
            }
            array_push($club_data_list,$b);
        }
//        print_r($club_data_list);
        return json($club_data_list);
    }
    //游戏列表
    public function game_list(){
        $result = Db::name("play")
            ->field("id,name")
            ->select();
        return json($result);

    }
    //新增用户
    public function New_users(){
        $create_time = input("param.create_time");
        $data = [
            'appid'=>APP_ID,
            'create_time'=>$create_time,
        ];
        $data = json_encode($data);
        $url =USER_HTTP."api/new_user.php";
        $curl = new \Curl();
        $result = $curl->vpost($url,$data);
        $josn = json_decode($result,true);
        // print_r($result);die;
        foreach($josn['data'] as $k=>$v){
            $josn['data'][$k]['nickname'] =  base64_decode($v['nickname']);
        }
        // print_r($josn);

        return ($josn['data']);

    }
    //充值
    public function Charge(){
//        充值用户总数	recharge_total_user	result
//        充值总金额	recharge_total_amount	result
//        充蓝钻用户数	recharge_gold_user	result
//        充蓝钻金额	   recharge_gold_amount	result
//        充黄钻会长数	recharge_diamond_user	result
//        充黄钻金额	recharge_diamond_amount	result
//        用户购卡用户数	recharge_user_card_user	result
//        用户购卡金额	recharge_user_card_amount	result
//        会长购卡会长数	recharge_club_card_user	result
//        会长购卡金额	recharge_club_card_amount	result

        $start_date =input("param.start_time");
        $end_date = input("param.end_time");
        $json = array('recharge_total_user','recharge_total_amount','recharge_gold_user','recharge_gold_amount','recharge_diamond_user','recharge_diamond_amount','recharge_user_card_user','recharge_user_card_amount','recharge_club_card_user','recharge_club_card_amount');
        $refer = json_encode($json);
        $sort = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $params = array(
            'refer'=>$refer,
            'product'=>PRODUCT,
            'start_date'=>"$start_date",
            'end_date'=>"$end_date",
            'sort'=>json_encode($sort)

        );
        $url =URL.'?'. http_build_query($params);
        $curl = new \Curl();
        $result = $curl->vget($url);
        $output = json_decode($result);
        $club_data_list = array();
        $club_data = array();
        $item = array();
        $a = array();
        $b = array();
        $c = array();
        foreach ($output->result as $k => $v) {
            array_push($a,(array)$v);
        }
        foreach($a as $k=>$v){
            if(!isset($item[$v['datetime']])){
                $item[$v['datetime']]=[$v];
            }else{
                array_push($item[$v['datetime']],$v);
            }
        }
        foreach ($item as $key =>$value){
            foreach ($value as $e =>$a){
                $b['datetime'] = $a['datetime'];
                if($a['refer'] == 'recharge_total_user') {
                    $b['recharge_total_user'] = $a['result'];//充值用户总数
                }
                if($a['refer'] == 'recharge_total_amount') {
                    $b['recharge_total_amount'] = round($a['result']/100,2);;//充值总金额
                }
                if($a['refer'] == 'recharge_gold_user') {
                    $b['recharge_gold_user'] = $a['result'];//充蓝钻用户数
                }
                if($a['refer'] == 'recharge_gold_amount') {
                    $b['recharge_gold_amount'] = round($a['result']/100,2);//充蓝钻金额
                }
                if($a['refer'] == 'recharge_diamond_user') {
                    $b['recharge_diamond_user'] = $a['result'];//充黄钻会长数
                }
                if($a['refer'] == 'recharge_diamond_amount') {
                    $b['recharge_diamond_amount'] = round($a['result']/100,2);//充黄钻金额
                }
                if($a['refer'] == 'recharge_user_card_user') {
                    $b['recharge_user_card_user'] = $a['result'];//用户购卡用户数
                }
                if($a['refer'] == 'recharge_user_card_amount') {
                    $b['recharge_user_card_amount'] =  round($a['result']/100,2);//用户购卡金额
                }
                if($a['refer'] == 'recharge_club_card_user') {
                    $b['recharge_club_card_user'] = $a['result'];//会长购卡会长数
                }
                if($a['refer'] == 'recharge_club_card_amount') {
                    $b['recharge_club_card_amount'] = round($a['result']/100,2);//会长购卡金额
                }

            }
            array_push($club_data_list,$b);
        }
        $recharge_gold_amount = 0;
        $recharge_diamond_amount = 0;
        $recharge_user_card_amount = 0;
        $recharge_club_card_amount = 0;
        foreach ($item as $key =>$value){
            foreach ($value as $e =>$a){
//                $b['datetime'] = $a['datetime'];

                if($a['refer'] == 'recharge_gold_amount') {
                    $recharge_gold_amount  += $a['result'];//充蓝钻金额
                }

                if($a['refer'] == 'recharge_diamond_amount') {
                    $recharge_diamond_amount  += $a['result'];//充黄钻金额

                }
                if($a['refer'] == 'recharge_user_card_amount') {
                    $recharge_user_card_amount += $a['result'];//用户购卡金额
                }
                if($a['refer'] == 'recharge_club_card_amount') {
                    $recharge_club_card_amount  += $a['result'];//会长购卡金额
                }

            }

        }
        $c['recharge_gold_amount'] = ['value'=>round($recharge_gold_amount/100,2),'name'=>'充蓝钻金额'];
        $c['recharge_diamond_amount']= ['value'=>round($recharge_diamond_amount/100,2),'name'=>'充黄钻金额'];
        $c['recharge_user_card_amount']= ['value'=>round($recharge_user_card_amount/100,2),'name'=>'用户购卡金额'];
        $c['recharge_club_card_amount']= ['value'=>round($recharge_club_card_amount/100,2),'name'=>'会长购卡金额'];
        array_push($club_data,$c);
        $result = new \stdClass();
        $result->Charge = $club_data_list;
        $result->amount_Charge = $club_data;
//        print_r($club_data_list);
        return json($result);

    }
    //数据金额
    public function amount_Charge(){
//        充蓝钻金额	   recharge_gold_amount	result
//        充黄钻金额	recharge_diamond_amount	result
//        用户购卡金额	recharge_user_card_amount	result
//        会长购卡金额	recharge_club_card_amount	result

        $start_date =input("param.start_time");
        $end_date = input("param.end_time");
        $json = array('recharge_gold_amount','recharge_diamond_amount','recharge_user_card_amount','recharge_club_card_amount');
        $refer = json_encode($json);
        $sort = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $params = array(
            'refer'=>$refer,
            'product'=>PRODUCT,
            'start_date'=>"$start_date",
            'end_date'=>"$end_date",
            'sort'=>json_encode($sort)

        );
        $url =URL.'?'. http_build_query($params);
        $curl = new \Curl();
        $result = $curl->vget($url);
        $output = json_decode($result);
        $club_data_list = array();
        $item = array();
        $a = array();
        $b = array();
        foreach ($output->result as $k => $v) {
            array_push($a,(array)$v);
        }
        foreach($a as $k=>$v){
            if(!isset($item[$v['datetime']])){
                $item[$v['datetime']]=[$v];
            }else{
                array_push($item[$v['datetime']],$v);
            }
        }
        $recharge_gold_amount = 0;
        $recharge_diamond_amount = 0;
        $recharge_user_card_amount = 0;
        $recharge_club_card_amount = 0;
        foreach ($item as $key =>$value){
            foreach ($value as $e =>$a){
//                $b['datetime'] = $a['datetime'];

                if($a['refer'] == 'recharge_gold_amount') {
                    $recharge_gold_amount  += $a['result'];//充蓝钻金额
                }

                if($a['refer'] == 'recharge_diamond_amount') {
                    $recharge_diamond_amount  += $a['result'];//充黄钻金额
                }
                if($a['refer'] == 'recharge_user_card_amount') {
                    $recharge_user_card_amount += $a['result'];//用户购卡金额
                }
                if($a['refer'] == 'recharge_club_card_amount') {
                    $recharge_club_card_amount  += $a['result'];//会长购卡金额
                }

            }

        }
        $b['recharge_gold_amount'] = round($recharge_gold_amount/100,2);
        $b['recharge_diamond_amount']= round($recharge_diamond_amount/100,2);
        $b['recharge_user_card_amount']= round($recharge_user_card_amount/100,2);
        $b['recharge_club_card_amount']= round($recharge_club_card_amount/100,2);
        array_push($club_data_list,$b);
        return json($club_data_list);

    }
    //会员卡销售
    public function vip_card_sale(){
//        会员卡买卖俱乐部详情	vip_card_club_detail
//        "club_id 俱乐部id
//        club_name 俱乐部名
//        club_region_id 俱乐部地域id
//        recharge_user_card_num 用户购卡数量(卖出)
//        recharge_club_card_num 会长购卡数量(买入)"
//        "recharge_user_card_amount 用户购卡金额(卖出)
//        recharge_club_card_amount 会长购卡金额(买入)"


        $start_date =input("param.start_time");
        $end_date = input("param.end_time");
        $json = array('vip_card_club_detail');
        $refer = json_encode($json);
        $sort = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $params = array(
            'refer'=>$refer,
            'product'=>PRODUCT,
            'start_date'=>"$start_date",
            'end_date'=>"$end_date",
            'sort'=>json_encode($sort)

        );
        $url =URL.'?'. http_build_query($params);
        $curl = new \Curl();
        $result = $curl->vget($url);
        $output = json_decode($result);
        $club_data_list = array();
        $item = array();
        $a = array();
        $b = array();
        foreach ($output->result as $k => $v) {
            array_push($a,(array)$v);
        }
        foreach($a as $k=>$v){
            if(!isset($item[$v['datetime']])){
                $item[$v['datetime']]=[$v];
            }else{
                array_push($item[$v['datetime']],$v);
            }
        }

    }
    //日活跃用户地域详情
    public function day_active_user(){
       /*
        day_active_user_region_detail
        "region_id 地域id
        day_active_user"*/
        $uid = Session::get("uid");
        $area = Db::name("users")->where("id",$uid)->find();
        $start_date =input("param.create_time");
        $end_date = input("param.create_time");
        $json = array('day_active_user_region_detail');
        $area_id = json_encode(explode(',',$area['area_id']));
        $refer = json_encode($json);
        $sort = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $params = array(
            'refer'=>$refer,
            'product'=>PRODUCT,
            'start_date'=>"$start_date",
            'end_date'=>"$end_date",
            'sort'=>json_encode($sort),
            'region_id'=>$area_id

        );
        $url =URL.'?'. http_build_query($params);
        $curl = new \Curl();
        $result = $curl->vget($url);
        $output = json_decode($result,true);
//        print_r($output);
        foreach ($output['result'] as $k=>$v){
            $area = Db::name('area')->where("aid",$v['region_id'])->find();
            $output['result'][$k]['area_name'] = $area['area_name'];


        }
        return json($output['result']);

    }
    //有效用户数地域详情
    public function valid_active_user(){
        /*
         day_active_user_region_detail
         "region_id 地域id
         day_active_user"*/
        $uid = Session::get("uid");
        $area = Db::name("users")->where("id",$uid)->find();
        $start_date =input("param.create_time");
        $end_date = input("param.create_time");
        $json = array('valid_active_user_region_detail');
        $area_id = json_encode(explode(',',$area['area_id']));
        $refer = json_encode($json);
        $sort = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $params = array(
            'refer'=>$refer,
            'product'=>PRODUCT,
            'start_date'=>"$start_date",
            'end_date'=>"$end_date",
            'sort'=>json_encode($sort),
            'region_id'=>$area_id

        );
        $url =URL.'?'. http_build_query($params);
        $curl = new \Curl();
        $result = $curl->vget($url);
        $output = json_decode($result,true);
//        print_r($output);
        foreach ($output['result'] as $k=>$v){
            $area = Db::name('area')->where("aid",$v['region_id'])->find();
            $output['result'][$k]['area_name'] = $area['area_name'];


        }
        return json($output['result']);

    }
    //开桌数地域详情
    public function create_room_num(){
        /*
         day_active_user_region_detail
         "region_id 地域id
         day_active_user"*/
        $uid = Session::get("uid");
        $area = Db::name("users")->where("id",$uid)->find();
        $start_date =input("param.create_time");
        $end_date = input("param.create_time");
        $json = array('create_room_num_region_detail');
        $area_id = json_encode(explode(',',$area['area_id']));
        $refer = json_encode($json);
        $sort = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $params = array(
            'refer'=>$refer,
            'product'=>PRODUCT,
            'start_date'=>"$start_date",
            'end_date'=>"$end_date",
            'sort'=>json_encode($sort),
            'region_id'=>$area_id

        );
        $url =URL.'?'. http_build_query($params);
        $curl = new \Curl();
        $result = $curl->vget($url);
        $output = json_decode($result,true);
//        print_r($output);
        foreach ($output['result'] as $k=>$v){
            $area = Db::name('area')->where("aid",$v['region_id'])->find();
            $output['result'][$k]['area_name'] = $area['area_name'];


        }
        return json($output['result']);

    }
    //完整桌数地域详情
    public function finish_room_num(){
        /*
         day_active_user_region_detail
         "region_id 地域id
         day_active_user"*/
        $uid = Session::get("uid");
        $area = Db::name("users")->where("id",$uid)->find();
        $start_date =input("param.create_time");
        $end_date = input("param.create_time");
        $json = array('finish_room_num_region_detail');
        $area_id = json_encode(explode(',',$area['area_id']));
        $refer = json_encode($json);
        $sort = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $params = array(
            'refer'=>$refer,
            'product'=>PRODUCT,
            'start_date'=>"$start_date",
            'end_date'=>"$end_date",
            'sort'=>json_encode($sort),
            'region_id'=>$area_id

        );
        $url =URL.'?'. http_build_query($params);
        $curl = new \Curl();
        $result = $curl->vget($url);
        $output = json_decode($result,true);
//        print_r($output);
        foreach ($output['result'] as $k=>$v){
            $area = Db::name('area')->where("aid",$v['region_id'])->find();
            $output['result'][$k]['area_name'] = $area['area_name'];


        }
        return json($output['result']);

    }


/*日活跃用户地域详情	day_active_user_region_detail	"region_id 地域id
day_active_user"
有效用户数地域详情	valid_active_user_region_detail	"region_id 地域id
valid_active_user"
开桌数地域详情	create_room_num_region_detail	"region_id 地域id
create_room_num"
完整桌数地域详情	finish_room_num_region_detail	"region_id 地域id
finish_room_num"
新增解散亲友圈数地域详情	change_club_num_region_detail	"region_id 地域id
create_club_num 创建亲友圈数
dissolve_club_num 解散亲友圈数"
消耗蓝钻数地域详情	reduce_gold_num_region_detail	"region_id 地域id
reduce_gold_num 消耗总钻石数
reduce_pay_gold_num 消耗购买蓝钻数
reduce_free_gold_num 消耗赠送蓝钻数"
会长购卡金额地域详情	club_recharge_card_amount_region_detail	"region_id 地域id
club_recharge_card_amount 会长购卡金额
club_recharge_card_num 会长购卡数量"*/
    //地区数据
    public function area_data(){
        /*日活跃用户	day_active_user
        日新增用户	day_added_user
        有效用户数	valid_active_user
        开桌数	create_room_num
        完整桌数	finish_room_num*/

        /*
         day_active_user_region_detail
         "region_id 地域id
         day_active_user"*/
        $club_data_list = array();
        $item = array();
        $a = array();
        $b = array();
        $c = array();
        $club_data_lists = array();
        $items = array();
        $as = array();
        $bs = array();
        $cs = array();
//        $region_id[] = Session::get("region_id");
//        $uid = Session::get("uid");
//        $area = Db::name("users")->where("id",$uid)->find();
//        $area_id = json_encode(explode(',',$area['area_id']));

        $start_date =input("param.start_time");
        $end_date = input("param.end_time");
        $area_id[] = input("param.area");
//        $start_date ='2018-10-30';
//        $end_date = '2018-10-30';
        $jsons = array(
                    'day_active_user',
                    'day_added_user',
                    'create_room_num',
        );
        $refers = json_encode($jsons);
        $sorts = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $param = array(
            'refer'=>$refers,
            'product'=>PRODUCT,
            'start_date'=>"$start_date",
            'end_date'=>"$end_date",
            'sort'=>json_encode($sorts),

        );
        $urls =URL.'?'. http_build_query($param);
        $curl = new \Curl();
        $results = $curl->vget($urls);
        $outputs = json_decode($results);
//        print_r($outputs);die;
        foreach ($outputs->result as $k => $v) {
            array_push($as,(array)$v);
        }
        $json = array(
                    'day_active_user_region_detail',
                    'valid_active_user_region_detail',
                    'create_room_num_region_detail',
                    'finish_room_num_region_detail',
                    'change_club_num_region_detail',
                    'reduce_gold_num_region_detail',
                    'club_recharge_card_amount_region_detail',
                    'diamond_recharge_region_detail'
             );
        $refer = json_encode($json);
        $sort = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $params = array(
            'refer'=>$refer,
            'product'=>PRODUCT,
            'start_date'=>"$start_date",
            'end_date'=>"$end_date",
            'sort'=>json_encode($sort),
            'region_id'=>json_encode($area_id)

        );
        $url =URL.'?'. http_build_query($params);
        $curl = new \Curl();
        $result = $curl->vget($url);
        $output = json_decode($result);

        foreach ($output->result as $k => $v) {
            array_push($a,(array)$v);
        }
        $array_merge = array_merge($a,$as);

        foreach($array_merge as $k=>$v){
            if(!isset($item[$v['datetime']])){
                $item[$v['datetime']]=[$v];
            }else{
                array_push($item[$v['datetime']],$v);
            }
        }

//        '日期', '新增', '日活', '总开桌数', '地区开桌数', '完成桌数', '有效日活', '新开俱乐部', '解散俱乐部', '总耗赠钻数', '会长买卡总额'
//        print_r($item);die;
        foreach ($item as $key => $value) {

            $b['day_added_user'] = 0;
            $b['day_active_user'] = 0;
            $b['create_room_num'] = 0;
            $b['create_room_num_region_detail'] = 0;
            $b['finish_room_num'] = 0;
            $b['day_active_user_region_detail']  = 0;
            $b['valid_active_user_region_detail']  = 0;
            $b['create_club_num']  = 0;
            $b['dissolve_club_num']  = 0;
            $b['reduce_free_gold_num'] = 0;
            $b['reduce_pay_gold_num']  = 0;
            $b['club_recharge_card_amount']  = 0;
            $b['club_recharge_card_num'] = 0;
            $b['recharge_diamond_amount']  = 0;//充值黄钻金额
            $b['recharge_diamond_num'] = 0;//充值黄钻数量

            foreach ($value as $e => $a) {
                $b['datetime'] = $a['datetime'];

                if($a['refer'] =='day_added_user'){
                    $day_added_user =  $a['result'];
                    $b['day_added_user'] =$a['result'];//新增
                }
                if($a['refer'] =='day_active_user'){
                    $day_active_user =  $a['result'];
                    $b['day_active_user'] = $a['result'] ;//日活
                }
                if($a['refer'] =='create_room_num'){
                    $create_room_num =  $a['result'];
                    $b['create_room_num'] = $a['result'];//开桌
                }
                if($a['refer'] =='create_room_num_region_detail'){
                    $create_room_num_region_detail =  $a['create_room_num'];
                    $b['create_room_num_region_detail'] = $a['create_room_num'];//地区开桌数
                }
                if($a['refer'] =='finish_room_num_region_detail'){
                    $finish_room_num =  $a['finish_room_num'];
                    $b['finish_room_num'] =  $a['finish_room_num']; ;//完整桌数
                }
                if($a['refer'] =='day_active_user_region_detail'){
                    $finish_room_num =  $a['day_active_user'];
                    $b['day_active_user_region_detail'] = $a['day_active_user'];;//日活
                }
                if($a['refer'] =='valid_active_user_region_detail'){

                    $b['valid_active_user_region_detail'] = $a['valid_active_user'];//有效日活
                }

                if($a['refer'] =='change_club_num_region_detail'){
                    $b['create_club_num'] = $a['create_club_num'];//新开俱乐部
                }
                if($a['refer'] =='change_club_num_region_detail'){
                    $b['dissolve_club_num'] = $a['dissolve_club_num'];//解散俱乐部
                }
                if($a['refer'] =='reduce_gold_num_region_detail'){
//                    $reduce_free_gold_num +=$a['reduce_free_gold_num'];
//                    $reduce_pay_gold_num +=$a['reduce_pay_gold_num'];
                    $b['reduce_free_gold_num'] = $a['reduce_free_gold_num'];
                    $b['reduce_pay_gold_num'] = $a['reduce_pay_gold_num'];
//                    $b['diamond'] = $a['reduce_pay_gold_num']+$a['reduce_free_gold_num'];//总耗赠钻数
//                    $reduce_pay_gold_num += $a['reduce_pay_gold_num'];//消耗购买蓝钻数
//                    $reduce_free_gold_num =
//                    $b['reduce_pay_gold_num']
                }
//                if($a['refer'] =='reduce_gold_num_region_det'){
//                    $b['reduce_free_gold_num'] = $a['reduce_free_gold_num'];//消耗赠送蓝钻数
//                }
                if($a['refer'] =='club_recharge_card_amount_region_detail'){
                    $b['club_recharge_card_amount'] = $a['club_recharge_card_amount']/100;//会长购卡金额
                }
                if($a['refer'] =='club_recharge_card_amount_region_detail'){
                    $b['club_recharge_card_num'] = $a['club_recharge_card_num'];//会长购卡数量
                }

                if($a['refer'] =='diamond_recharge_region_detail'){
                    $b['recharge_diamond_amount'] = $a['recharge_diamond_amount']/100;//充值黄钻金额
                }
                if($a['refer'] =='diamond_recharge_region_detail'){
                    $b['recharge_diamond_num'] = $a['recharge_diamond_num'];//充值黄钻数量
                }
            }

            if(!isset($b['day_added_user'])){
                $b['day_added_user'] = 0;
            }
            if(!isset($b['day_active_user'])){
                $b['day_active_user'] = 0;
            }
            if(!isset($b['create_room_num_region_detail'])){
                $b['create_room_num_region_detail'] = 0;
            }
            if(!isset($b['finish_room_num'])){
                $b['finish_room_num'] = 0;
            }
            if(!isset($b['create_club_num'])){
                $b['create_club_num'] = 0;
            }
            if(!isset($b['day_active_user_region_detail'])){
                $b['day_active_user_region_detail'] = 0;
            }
            if(!isset($b['dissolve_club_num'])){
                $b['dissolve_club_num'] = 0;
            }
            if(!isset($b['reduce_pay_gold_num'])){
                $b['reduce_pay_gold_num'] = 0;
            }
            if(!isset($b['reduce_free_gold_num'])){
                $b['reduce_free_gold_num'] = 0;
            }

            if(!isset($b['reduce_free_gold_num'])){
                $b['reduce_free_gold_num'] = 0;
            }
            if(!isset($b['reduce_pay_gold_num'])){
                $b['reduce_pay_gold_num'] = 0;
            }
            if(!isset($b['create_room_num'])){
                $b['create_room_num'] = 0;
            }
            if(!isset($b['club_recharge_card_amount'])){
                $b['club_recharge_card_amount'] = 0;
            }
            if(!isset($b['club_recharge_card_num'])){
                $b['club_recharge_card_num'] = 0;
            }
            if(!isset($b['recharge_diamond_amount'])){
                $b['recharge_diamond_amount'] = 0;
            }
            if(!isset($b['recharge_diamond_num'])){
                $b['recharge_diamond_num'] = 0;
            }


            array_push($club_data_list,$b);
        }
        $ctime_str = array();
        foreach($club_data_list as $key=>$v){
            $club_data_list[$key]['ctime_str'] = strtotime($v['datetime']);
            $ctime_str[] = $club_data_list[$key]['ctime_str'];
        }
        array_multisort($ctime_str,SORT_DESC,$club_data_list);
//        uasort($club_data_list,'my_sort');
//        print_r($b);

        return json($club_data_list);

    }
    //地区数据(总数)
    public function area_data_sum(){
        /*日活跃用户	day_active_user
        日新增用户	day_added_user
        有效用户数	valid_active_user
        开桌数	create_room_num
        完整桌数	finish_room_num*/

        /*
         day_active_user_region_detail
         "region_id 地域id
         day_active_user"*/
        $club_data_list = array();
        $item = array();
        $a = array();
        $b = array();
        $c = array();
        $club_data_lists = array();
        $items = array();
        $as = array();
        $bs = array();
        $cs = array();

        $start_date =input("param.start_time");
        $end_date = input("param.end_time");
//        $start_date ='2018-10-28';
//        $end_date ='2018-10-31';
        $area_id[] = input("param.area");
        $startdate=strtotime($start_date);
        $enddate=strtotime($end_date );
        $days=round(($enddate-$startdate)/3600/24)+1 ;
//        print_r();die;
        $uid = Session::get("uid");
        $area = Db::name("users")->where("id",$uid)->find();
        $area_id = json_encode(explode(',',$area['area_id']));
        $curl = new \Curl();
        $json = array(
            'day_active_user_region_detail',
            'valid_active_user_region_detail',
            'create_room_num_region_detail',
            'finish_room_num_region_detail',
            'change_club_num_region_detail',
            'reduce_gold_num_region_detail',
            'club_recharge_card_amount_region_detail',
            'diamond_recharge_region_detail'
        );
        $refer = json_encode($json);
        $sort = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $params = array(
            'refer'=>$refer,
            'product'=>PRODUCT,
            'start_date'=>"$start_date",
            'end_date'=>"$end_date",
            'sort'=>json_encode($sort),
            'region_id'=>$area_id,
//            'region_id'=>json_encode([4])

        );
        $url =URL.'?'. http_build_query($params);
        $curl = new \Curl();
        $result = $curl->vget($url);
        $output = json_decode($result);
        foreach ($output->result as $k => $v) {
            array_push($a,(array)$v);
        }

        foreach($a as $k=>$v){
            if(!isset($item[$v['region_id']])){
                $item[$v['region_id']]=[$v];
            }else{
                array_push($item[$v['region_id']],$v);
            }
        }

//        print_r($item);die;
//        '日期', '新增', '日活', '总开桌数', '地区开桌数', '完成桌数', '有效日活', '新开俱乐部', '解散俱乐部', '总耗赠钻数', '会长买卡总额'
        foreach ($item as $key => $value) {
            $create_room_num_region_detail = 0;
            $finish_room_num = 0;
            $day_active_user_region_detail = 0;
            $create_club_num = 0;
            $dissolve_club_num = 0;
            $reduce_free_gold_num= 0;
            $reduce_pay_gold_num = 0;
            $club_recharge_card_amount= 0;
            $club_recharge_card_num= 0;
            $valid_active_user_region_detail =0;
            $recharge_diamond_num =0;
            $b['create_room_num_region_detail'] =0;
            $b['finish_room_num'] =0;
            $b['day_active_user_region_detail'] =0;
            $b['valid_active_user_region_detail'] =0;
            $b['create_club_num'] =0;
            $b['dissolve_club_num'] =0;
            $b['reduce_free_gold_num'] =0;
            $b['reduce_pay_gold_num'] =0;
            $b['club_recharge_card_amount'] =0;
            $b['club_recharge_card_num'] =0;
            $b['recharge_diamond_num'] =0;
            foreach ($value as $e => $a) {
                $b['region_id'] = $a['region_id'];
                if($a['refer'] =='create_room_num_region_detail'){
                    $create_room_num_region_detail += $a['create_room_num'];
                    $b['create_room_num_region_detail'] = $create_room_num_region_detail;//地区开桌数
                }
                if($a['refer'] =='finish_room_num_region_detail'){
                    $finish_room_num += $a['finish_room_num'];
                    $b['finish_room_num'] = $finish_room_num;//完整桌数
                }
                if($a['refer'] =='day_active_user_region_detail'){
                    $day_active_user_region_detail += $a['day_active_user'];
                    $b['day_active_user_region_detail'] =round( $day_active_user_region_detail/$days,2);//有效日活
                }
                if($a['refer'] =='valid_active_user_region_detail'){
                    $valid_active_user_region_detail += $a['valid_active_user'];
                    $b['valid_active_user_region_detail'] = round($valid_active_user_region_detail/$days,2);//有效日活
                }
                if($a['refer'] =='change_club_num_region_detail'){
                    $create_club_num += $a['create_club_num'];
                    $b['create_club_num'] = $create_club_num ;//新开俱乐部
                }
                if($a['refer'] =='change_club_num_region_detail'){
                    $dissolve_club_num += $a['dissolve_club_num'];
                    $b['dissolve_club_num'] = $dissolve_club_num ;//解散俱乐部
                }
                if($a['refer'] =='reduce_gold_num_region_detail'){
                    $reduce_free_gold_num +=$a['reduce_free_gold_num'];
                    $reduce_pay_gold_num +=$a['reduce_pay_gold_num'];
                    $b['reduce_free_gold_num'] = $reduce_free_gold_num;
                    $b['reduce_pay_gold_num'] = $reduce_pay_gold_num;
//                    $b['diamond'] = $a['reduce_pay_gold_num']+$a['reduce_free_gold_num'];//总耗赠钻数
//                    $b['reduce_free_gold_num'] = $a['reduce_free_gold_num'];//消耗赠送蓝钻数
//                    $reduce_pay_gold_num += $a['reduce_pay_gold_num'];//消耗购买蓝钻数
                }
                if($a['refer'] =='club_recharge_card_amount_region_detail'){
                    $club_recharge_card_amount += $a['club_recharge_card_amount'];
                    $b['club_recharge_card_amount'] = $club_recharge_card_amount/100;//会长购卡金额
                }
                if($a['refer'] =='club_recharge_card_amount_region_detail'){
                    $club_recharge_card_num += $a['club_recharge_card_num'];
                    $b['club_recharge_card_num'] = $club_recharge_card_num;//会长购卡数量
                }
                if($a['refer'] =='diamond_recharge_region_detail'){
                    $recharge_diamond_num += $a['recharge_diamond_num'];
                    $b['recharge_diamond_num'] = $recharge_diamond_num;//会长购卡数量
                }
            }

            if(!isset($b['day_added_user'])){
                $b['day_added_user'] = 0;
            }
            if(!isset($b['day_active_user'])){
                $b['day_active_user'] = 0;
            }
            if(!isset($b['create_room_num_region_detail'])){
                $b['create_room_num_region_detail'] = 0;
            }
            if(!isset($b['finish_room_num'])){
                $b['finish_room_num'] = 0;
            }
            if(!isset($b['create_club_num'])){
                $b['create_club_num'] = 0;
            }
            if(!isset($b['day_active_user_region_detail'])){
                $b['day_active_user_region_detail'] = 0;
            }
            if(!isset($b['dissolve_club_num'])){
                $b['dissolve_club_num'] = 0;
            }
            if(!isset($b['reduce_pay_gold_num'])){
                $b['reduce_pay_gold_num'] = 0;
            }
            if(!isset($b['reduce_free_gold_num'])){
                $b['reduce_free_gold_num'] = 0;
            }
            if(!isset($b['diamond'])){
                $b['diamond'] = 0;
            }
            if(!isset($b['create_room_num'])){
                $b['create_room_num'] = 0;
            }
            if(!isset($b['club_recharge_card_amount'])){
                $b['club_recharge_card_amount'] = 0;
            }
            if(!isset($b['club_recharge_card_num'])){
                $b['club_recharge_card_num'] = 0;
            }
            if(!isset($b['recharge_diamond_num'])){
                $b['recharge_diamond_num'] = 0;
            }

            array_push($club_data_list,$b);
        }
        foreach ($club_data_list as $k=>$v){
            $areas = Db::name('area')->where("aid",$v['region_id'])->find();
            $club_data_list[$k]['area_name'] = $areas['area_name'];
        }
//        uasort($club_data_list,'my_sort');
//        print_r($club_data_list);
        return json($club_data_list);

    }
    //高级会长
    public function Senior_president(){
        $item = [];
        $a = [];
        $b = [];
        $start_date =input("param.start_time");
        $end_date = input("param.end_time");
        $uid = input("param.uid");

        $json = array('rebate_user_detail');
        $refer = json_encode($json);
        $sort = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $params = array(
            'refer'=>$refer,
            'product'=>PRODUCT,
            'start_date'=>"$start_date",
            'end_date'=>"$end_date",
            'user_id'=>$uid,
            'sort'=>json_encode($sort)

        );
        $url =URL.'?'. http_build_query($params);
        $curl = new \Curl();
        $result = $curl->vget($url);
        $output = json_decode($result,true);
        foreach($output['result'] as $k=>$v){
            if(!isset($item[$v['user_id']])){
                $item[$v['user_id']]=[$v];
            }else{
                array_push($item[$v['user_id']],$v);
            }
        }

        foreach ($item as $k=>$v){
            $sum = 0;
            foreach ($v as $key=>$value){
                if($k == $value['user_id']){
                    $a['user_id'] =$value['user_id'];
                    $a['user_name'] =$value['user_name'];
                    $sum +=$value['high_level_club_rebate_num'];
                }
                $a['start_room_rebate_num'] = round($sum/100,2);
            }
            array_push($b,$a);
        }
        return json($b);
    }
    //高级会长(详情)
    public function Senior_president_details(){
        $item = [];
        $a = [];
        $b = [];
        $start_date =input("param.start_time");
        $end_date = input("param.end_time");
        $uid = input("param.uid");

        $json = array('high_level_club_rebate_club_detail');
        $refer = json_encode($json);
        $sort = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $params = array(
            'refer'=>$refer,
            'product'=>PRODUCT,
            'start_date'=>"$start_date",
            'end_date'=>"$end_date",
            'user_id'=>$uid,
            'sort'=>json_encode($sort)

        );
        $url =URL.'?'. http_build_query($params);
        $curl = new \Curl();
        $result = $curl->vget($url);
        $output = json_decode($result,true);
        foreach($output['result'] as $k=>$v){
            if(!isset($item[$v['do_rebate_club_id']])){
                $item[$v['do_rebate_club_id']]=[$v];
            }else{
                array_push($item[$v['do_rebate_club_id']],$v);
            }
        }
//        print_r($item);die;
//user_id 高级会长用户id
//user_name 高级会长用户名
//do_rebate_club_id 下级亲友圈id
//do_rebate_club_name 下级亲友圈名
//do_rebate_club_region_id 下级亲友圈地域id
//high_level_club_rebate_num 高级会长返利
        foreach ($item as $k=>$v){
            $high_level_club_rebate_num = 0;
            $a['high_level_club_rebate_num']=0;
            foreach ($v as $key=>$value){
//                if($k == $value['user_id']){
                    $a['user_id'] =$value['user_id'];
                    $a['user_name'] =$value['user_name'];
                    $a['do_rebate_club_id'] =$value['do_rebate_club_id'];
                    $a['do_rebate_club_name'] =$value['do_rebate_club_name'];
                    $a['do_rebate_club_region_id'] =$value['do_rebate_club_region_id'];
                    $high_level_club_rebate_num += $value['high_level_club_rebate_num'];
//                    print_r($a['high_level_club_rebate_num']);
                    $a['high_level_club_rebate_num'] = $high_level_club_rebate_num/100;
//                    $sum +=$value['start_room_rebate_num'];
//                }
//                $a['start_room_rebate_num'] = $sum;
            }
            array_push($b,$a);
        }
        $where = [];
        foreach ($b as $k=>$v){
            $senior_president = $v['user_id'];
            $cid = $v['do_rebate_club_id'];
            $where['senior_president'] = $v['user_id'];
            $where['cid'] = $v['do_rebate_club_id'];

            $result = Db::name('club')->where($where)->find();
            if($result){
                $b[$k]['type'] = 1;
            }else{
                $b[$k]['type'] = 0;
            }

        }
//        print_r($b);
        return json($b);
    }
    //高级会长(收益)
    public function Senior_president_Profit(){
        $item = [];
        $a = [];
        $b = [];
        $start_date =input("param.start_time");
        $end_date = input("param.end_time");
        $uid = input("param.uid");
        $club_id = input("param.club_id");

        $json = array('high_level_club_rebate_club_detail');
        $refer = json_encode($json);
        $sort = [
            'columns'=>['datetime'],
            'order'=>["desc"]
        ];
        $params = array(
            'refer'=>$refer,
            'product'=>PRODUCT,
            'start_date'=>"$start_date",
            'end_date'=>"$end_date",
            'user_id'=>$uid,
            'do_rebate_club_id'=>$club_id,
            'sort'=>json_encode($sort)

        );
        $url =URL.'?'. http_build_query($params);
        $curl = new \Curl();
        $result = $curl->vget($url);
        $output = json_decode($result,true);
        foreach($output['result'] as $k=>$v){
            if(!isset($item[$v['do_rebate_club_id']])){
                $item[$v['do_rebate_club_id']]=[$v];
            }else{
                array_push($item[$v['do_rebate_club_id']],$v);
            }
        }
//        print_r($item);die;
//user_id 高级会长用户id
//user_name 高级会长用户名
//do_rebate_club_id 下级亲友圈id
//do_rebate_club_name 下级亲友圈名
//do_rebate_club_region_id 下级亲友圈地域id
//high_level_club_rebate_num 高级会长返利
        foreach ($item as $k=>$v){
            foreach ($v as $key=>$value){
//                if($k == $value['user_id']){
                    $a['datetime'] =$value['datetime'];
                    $a['user_id'] =$value['user_id'];
                    $a['user_name'] =$value['user_name'];
                    $a['do_rebate_club_id'] =$value['do_rebate_club_id'];
                    $a['do_rebate_club_name'] =$value['do_rebate_club_name'];
                    $a['do_rebate_club_region_id'] =$value['do_rebate_club_region_id'];
                    $a['high_level_club_rebate_num'] =$value['high_level_club_rebate_num']/100;
//                    $sum +=$value['start_room_rebate_num'];
//                }
//                $a['start_room_rebate_num'] = $sum;
            }
            array_push($b,$a);
        }
//        print_r($b);
        return json($b);
        }

    }
